﻿#region

using System;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class OperatorRegistrationForm : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] roles = Roles.GetRolesForUser(Page.User.Identity.Name);

                if (!roles.Contains("Manager"))
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }

        protected void tbEMail_OnTextChanged(object sender, EventArgs e)
        {
            cvEMail.Validate();
        }

        protected void cvEMail_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = !StaffBLL.IsEMailTaken(tbEMail.Text);
        }

        protected void btnRegister_OnClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var staffId = StaffBLL.RegisterStaff(tbFName.Text, tbLName.Text, tbEMail.Text, tbTelephone.Text,
                    tbAddress.Text, "Administrator", null);
                AdministratorBLL.RegisterAdmin(staffId, tbUsername.Text, tbPassword.Text, "Operator");
                Response.Redirect("Operators.aspx");
            }
        }

        protected void tbUsername_OnTextChanged(object sender, EventArgs e)
        {
            cvUsername.Validate();
        }

        protected void cvUsername_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = !UserBLL.IsUsernameTaken(tbUsername.Text);
        }
    }
}